<template>
  <div class="page-login">
    <div class="logo">
      <img src="https://assets.maizuo.com/h5/mz-auth/public/app/img/logo.19ca0be.png">
    </div>
    <div class="login-form">
      <div class="form-group">
        <input placeholder="用户名" class="input-control" type="text" v-model="username" />
      </div>
      <div class="form-group">
        <input placeholder="密码" class="input-control" type="password" v-model="password" />
      </div>

      <div class="submit login-btn" @click="handleLogin">
        <span>登录</span>
      </div>
    </div>
  </div>
</template>

<script>
import Axios from 'axios'

export default {
  name: 'Login',

  data () {
    return {
      username: '',
      password: '',

      // 正在登录中...
      loading: false
    }
  },

  methods: {
    /**
     * 登录
     */
    handleLogin () {
      if (this.loading) {
        return
      }

      if (!this.username || !this.password) {
        this.$toast('用户名与密码不能为空')
        return
      }

      this.loading = true

      // 发送ajax请求
      Axios.post('http://localhost:3000/login', {
        username: this.username,
        password: this.password
      }).then(response => {
        const { data } = response
        if (data.code === 0) {
          // 1. 提示成功了
          this.$toast('登录成功')

          // 2. 用户信息与token给存储起来
          this.$store.commit('SET_USER_INFO_AND_TOKEN', data.data)

          // 3. 跳转回个人中心页面 （编程式导航）
          setTimeout(() => {
            this.$router.replace('/center')
          }, 1500)

          this.loading = false
        }
      })
    }
  }
}
</script>

<style lang="scss">
@import '~@/assets/styles/mixins.scss';

.page-login {
  height: 100%;
  font-size: 16px;
  background: #fff;
  overflow: hidden;
  .logo {
    margin: 79px auto 40px;
    text-align: center;
    height: 60px;
    line-height: 60px;
    img {
      height: 60px;
    }
  }
  .login-form {
    .form-group {
      @include border-bottom;
      line-height: 55px;
      margin: 0 25px;
      position: relative;
      .input-control {
        height: 15px;
        line-height: 15px;
        padding: 20px 0;
        width: 100%;
        font-size: 15px;
        color: #191a1b;
        border: 0;
        outline-width: 0;
      }
    }
    .submit {
      line-height: 45px;
      font-size: 16px;
      margin: 70px 25px 0;
      border-radius: 3px;
      text-align: center;
      background-color: #ff5f16;
      height: 44px;
      color: #fff;
      border: none;
    }
  }
}
</style>
